<!DOCTYPE html>
<html>
<head>
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/common.js"></script>
    <script>
        //创建一个选项卡的方法
        function createTab(index, obj) {
            /*<li class="nav-item" data-index="xx">
            <a data-tid="xx" class="nav-link" data-toggle="pill" href="#pills-xx">oo</a>
                    </li>*/
            var a = $("<a class='nav-link' data-toggle='pill'></a>").attr("data-tid", obj.id)
                .attr("href", "#pills-" + obj.id).html(obj.name);
            return $("<li class='nav-item'></li>").attr("data-index", index).append(a);
        }

        $(function () {
            var tabs = []; //所有选项卡的保存信息,用于翻页
            var tab; //当前选中的选项卡

            //1:渲染攻略推荐
            $.get("/travels/commends", {type: 3}, function (data) {
                $("#pills-top").renderValues(data[0], {
                    handle: function (ele, val) {
                        ele.href = "/travelContent.html?id=" + val;
                    }
                });
            });

            //2:渲染推荐大攻略
            $.get("/strategies/commends", {state: 2}, function (data) {
                setTimeout(function () {
                    $(".strategyCommend").renderValues({list: data}, {
                        handle: function (ele, val) {
                            ele.href = "/strategyCatalogs.html?id=" + val;
                        }
                    });
                },20)
            });

            //3:渲染顶部的区域推荐
            $.get("/regions/hots", {state: 1}, function (data) {
                //不满足jrender渲染条件
                $.each(data, function (index, ele) {
                    createTab(index, ele).appendTo("#pills-tab");
                    //给生成的选项卡绑定点击事件
                    $.get("/regions/" + ele.id + "/strategies",
                        {pageSize: 2},
                        function (data) {
                            $(data).appendTo("#pills-tabContent");
                            //保存选项卡信息. 当前页/总页数/区域id
                            tabs[index] = {currentPage: 1, pages: $(data).data("pages"), regionId: ele.id};
                        }, "html");
                });

                //点击选项卡,却换分页信息
                $("#pills-tab li[data-index]").click(function () {
                    var index = $(this).data("index");
                    tab = tabs[index]; //获取当前选中的选项卡信息
                });
            });
            //分页
            $(window).scroll(function () {
                if (isFull()) {
                    if (tab) {
                        //判断当前不是最后一页
                        if (tab.currentPage < tab.pages) {
                            $.get("/regions/" + tab.regionId + "/strategies",
                                {currentPage: ++tab.currentPage, pageSize: 2},
                                function (data) {
                                    $(data).appendTo("#pills-" + tab.regionId + " .classify");
                                }, "html");
                        } else {
                            //提示到底了
                            $(document).dialog({
                                type: "notice",
                                infoText: "没有下一页,到底了",
                                autoClose: 1500,
                                position: "bottom"
                            });
                        }
                    }
                }
            });
        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="找攻略">
            </div>
        </div>
    </div>
</div>

<!--顶部-->
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>
    <!-- 渲染的区域,此时不满足jrender渲染条件,只能手动使用DOM方式渲染
    <li class="nav-item" data-index="0">
        <a class="nav-link" data-toggle="pill" href="#pills-1">亚洲</a>
    </li>
    -->
</ul>
<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">
            <a render-key="travelId" render-fun="handle">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>
        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>推荐</h6>
            <div class="row hot" render-loop="list">

                <div class="col-4">
                    <a render-key="list.id" render-fun="handle">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>

            </div>
        </div>
    </div>

    <!--这里填充每个标签页的数据-->
</div>
</body>
</html>